﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data;
using MaxLeaf.DB;

namespace MaxLeaf.DAL
{
    public class ICPrcPlyDALBuilder : BaseDALBuilder
    {


        public ICPrcPlyDALBuilder()
            : base()
        {
            K3DAL = new K3.ICPrcPlyDAL();

            WebDAL =  new Web.mary_icprcplyDAL();

        }
        public bool CheckHdr(ServerInfo orgServer, string strFInterID)
        {
            using (DBAccess dbOrgCon = new DBAccess(orgServer))
            {
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("ICPrcPly", new Dictionary<string, object>() { { "FInterID", strFInterID } }, "FPlyType");
                if (drOrgData == null)
                {
                    LogManager.Debug(string.Format("ICPrcPly {0} not exist", strFInterID));
                    return false;
                }

                //只分发客户类别
                if (!CvtFunc.ToString(drOrgData["FPlyType"]).Equals("PrcAsm2", StringComparison.InvariantCultureIgnoreCase))
                {

                    LogManager.Debug(string.Format("ICPrcPly {0} FPlyType {1} 不分发", strFInterID, drOrgData["FPlyType"]));
                    return false;
                }

            }
            return true;
        }
        protected override bool ValidCheckOrgData(ServerInfo orgServer, DataRowView drvLog)
        {
            if (!orgServer.MasterDB)
            {
                //主账套分发
                return false;
            }
            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return true;
            }
            if (!CheckHdr(orgServer, CvtFunc.ToString(drvLog[DEL.LogDEL.KeyValue])))
            {
                return false;
            }


            return base.ValidCheckOrgData(orgServer, drvLog);
        }

        protected override bool ValidCheck(ServerInfo orgServer, ServerInfo destServer, DataRowView drvLog)
        {


            if (!CheckHdr(orgServer, destServer, CvtFunc.ToString(drvLog[DEL.LogDEL.KeyValue])))
            {
                return false;
            }


            return base.ValidCheck(orgServer, destServer, drvLog);
        }



        internal bool CheckHdr(ServerInfo orgServer, ServerInfo destServer, string strFInterID)
        {
            if (orgServer.CityDBs.Count > 0)
            {
                using (DBAccess dbOrgCon = new DBAccess(orgServer))
                {
                    DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("ICPrcPly", new Dictionary<string, object>() { { "FInterID", strFInterID } }, "FNumber");
                    if (drOrgData == null)
                    {
                        LogManager.Debug(string.Format("ICPrcPly {0} not exist", strFInterID));
                        return false;
                    }

                    string strFNumber = CvtFunc.ToString(drOrgData["FNumber"]);
                    if (destServer.Type == ServerType.Web)
                    {
                        if (!(strFNumber.Equals("99901", StringComparison.InvariantCultureIgnoreCase)))
                        {

                            LogManager.Debug(string.Format("ICPrcPly {0} FNumber {1} 不分发网站", strFInterID, strFNumber));
                            return false;
                        }
                    }
                    else if (destServer.Type == ServerType.K3)
                    {
                        if ((strFNumber.Equals("99901", StringComparison.InvariantCultureIgnoreCase)))
                        {

                            LogManager.Debug(string.Format("ICPrcPly {0} FNumber {1} 不分发K3", strFInterID, strFNumber));
                            return false;
                        }
                    }

                }
            }
            return true;
        }
    }
}